package com.hd.appcommon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hd.appcommon.entity.Door;
import com.hd.appcommon.entity.DoorVo;
import com.hd.appcommon.entity.Room;
import com.hd.appcommon.entity.RoomMessageUpload;
import com.hd.appcommon.mapper.DoorMapper;
import com.hd.appcommon.mapper.RoomMapper;
import com.hd.appcommon.service.DoorService;
import com.hd.appcommon.service.RoomService;
import com.hd.appcommon.utils.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @projectName:    entrance_guard 
 * @package:        com.hd.appcommon.service.impl
 * @className:      RoomServiceImpl
 * @author:     otto
 * @description:  
 * @date:    2023/7/24 10:30
 * @version:    1.0
 */ 

@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements RoomService {
    @Autowired
    private DoorMapper doorMapper;
    @Autowired
    private DoorService doorService;
    @Override
    public List<DoorVo> getDoorList(String roomId) {
        return doorMapper.getDoorList(roomId);
    }

    @Override
    public void addDoorId(Integer roomId, Integer doorId) {
        doorMapper.addDoorId(roomId, String.valueOf(doorId));
    }

    @Override
    public String getDoorIdListByDoorId(String doorId) {
        return doorMapper.getDoorIdListByDoorId(doorId);
    }

    @Override
    public void updateUserIdListByDoorId(String doorId, String newDoorIdList) {
        doorMapper.updateUserIdListByDoorId(doorId,newDoorIdList);
    }

    @Override
    public Boolean upLoadUserMessage(RoomMessageUpload roomMessageUpload) {
        System.out.println("上传数据");
        // 先确认房间信息是否存在，索引、楼宇名称、房间名称，存在则记录房间id、不存在则增加并返回房间id
        Room room = this.getOne(new QueryWrapper<Room>()
                .eq("building_name",roomMessageUpload.getBuildingName())
                .eq("room_name",roomMessageUpload.getRoomName())
                .last("LIMIT 1"));
        Integer roomId = null;
        if(room!=null){
            roomId = room.getId();
            room.setUserIdList(roomMessageUpload.getUserIdList());
            this.update(new UpdateWrapper<Room>()
                    .eq("building_name",roomMessageUpload.getBuildingName())
                    .eq("room_name",roomMessageUpload.getRoomName())
                    .last("LIMIT 1")
                    .set("user_id_list",roomMessageUpload.getUserIdList()));
        }
        else{
            Room roomNew = new Room()
                    .setRoomName(roomMessageUpload.getRoomName())
                    .setBuildingName(roomMessageUpload.getBuildingName())
                    .setUserIdList(roomMessageUpload.getUserIdList());
            this.save(roomNew);
            roomId = roomNew.getId();
        }
        // 再确定设备信息是否存在，索引、人脸识别设备id、锁设备门点id,同上
        Door door = doorService.getOne(new QueryWrapper<Door>()
                .eq("device_id",roomMessageUpload.getDeviceId())
                .eq("door_lock_code",roomMessageUpload.getDoorLockCode())
                .last("LIMIT 1"));
        if(door==null){
            Door doorNew = new Door()
                    .setDoorName(roomMessageUpload.getDoorName())
                    .setDeviceId(roomMessageUpload.getDeviceId())
                    .setPass(roomMessageUpload.getPass())
                    .setDoorLockCode(roomMessageUpload.getDoorLockCode())
                    .setRoomId(roomId);
            doorService.save(doorNew);
            door = doorNew;
        }
        else{
            if(!door.getRoomId().equals(roomId)){
                doorService.update(new UpdateWrapper<Door>().eq("door_id",door.getDoorId()).set("room_id",roomId));
            }
        }
        // 由于添加用户权限需要用户的人脸图片，因此上传用户权限分开上传
        String doorIdsList = null;
        Integer doorId = door.getDoorId();
        if(StringUtils.isNotBlank((room.getDoorIdList()))){
            List<String> originDoorIdList = Arrays.asList(room.getDoorIdList().split(","));

            originDoorIdList.stream().filter(r-> (!r.equals(doorId))).collect(Collectors.toList());
            doorIdsList = StringUtils.join(originDoorIdList,",");
            doorIdsList.concat(String.valueOf(doorId));
        }
        else{
            doorIdsList = String.valueOf(doorId);
        }

        this.update(new UpdateWrapper<Room>().eq("id",roomId).set("door_id_list",doorIdsList));
        return true;
    }


}
